//1.导入网络模块
const  http = require('http')
const path = require('path')

//解析url
const url = require('url')

//2.创建服务对象
const app = http.createServer()

//3.监听客户端的请求
app.on('request', (request, response) => {
    // // 获取请求报文
    // console.log(request.headers)

    // // 获取请求方式
    // console.log(request.method)

    // // 获取请求地址
    // console.log(request.url)

    // 设置响应头信息
    response.writeHead(200, {
        'Content-Type': 'text/html;charset=UTF8'
    })


    // 解析url地址
    const {searchParams, pathname} = new URL(request.url, 'http://127.0.0.1:3000')
    console.log(request.headers.referer)


    // console.log(searchParams.get('name'))
    // console.log(searchParams.get('age'))
    // console.log(pathname)
    // console.log(request)

    if(pathname == '/index') {
        response.end('<h1>欢迎来到主页</h1>')
    }else if (pathname == '/list'){
        response.end('<h1>welcome listpage</h1>')
    }else {
        response.end('<h1>Not Find</h1>')
    }
    


})

app.listen(3000, () => {
    console.log('服务启动成功,端口号3000')
})
